package controller;

import model.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserChangeAddress", value = "/user_changeaddress")
public class UserChangeAddressServlet extends HttpServlet {
    UserService us = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;Charset=utf-8");
        User loginUser = (User) request.getSession().getAttribute("user");
        loginUser.setAddress(request.getParameter("address"));
        loginUser.setName(request.getParameter("name"));
        loginUser.setPhone(request.getParameter("phone"));
        us.updateUserAddress(loginUser);
        request.setAttribute("msg", "收件信息更新成功！");
        request.getSession().setAttribute("user", loginUser);
        System.out.println(1);
        request.getRequestDispatcher("user_center.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
